package com.yw.atp.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.yw.atp.model.Appointment;
import com.yw.atp.service.AppointmentService;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.util.DateUtil;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("appointmentAction")
public class AppointmentAction extends ActionBase implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	@Autowired
    private PersistenceService service;
    @Autowired
    private AppointmentService appointmentService;
	HttpServletRequest request;
	public String test;
	public String p_id = null;
	public String f_t = null;
	public String t_t = null;
	public String type = null;
	public String loc = null;
	public String fromDate = null;
	public String toDate = null;
	public String model = null;
	public String s = null;
	
	public String test() throws JSONException{
		    try{
		    	JSONObject js = new JSONObject();
		    	js.put("status","000");
		    	if("select".equals(type)){
		    		List<Appointment> appms = appointmentService.getAppointments(new Date(Long.valueOf(fromDate).longValue()),new Date(Long.valueOf(toDate).longValue()));
		    		js.put("result",Appointment.capture(appms));
		    	}else if("add".equals(type)){
		    		Appointment app = new Appointment(DateUtil.parseDate(f_t),DateUtil.parseDate(t_t),s,loc);
		    		appointmentService.save(app);
		    		js.put("result",new JSONArray().put(app.capture()));
		    	}else if("edit".equals(type)){
		    		Appointment app = new Appointment(DateUtil.parseDate(f_t),DateUtil.parseDate(t_t),s,loc);
		    		app.setId(Integer.valueOf(p_id));
		    		appointmentService.save(app);
		    		js.put("result",new JSONArray().put(app.capture()));
		    	}else if("del".equals(type)){
		    		Appointment app = new Appointment(DateUtil.parseDate(f_t),DateUtil.parseDate(t_t),s,loc);
		    		app.setId(Integer.valueOf(p_id));
		    		appointmentService.delet(app);
		    		js.put("result",new JSONArray().put(new JSONObject()));
		    	}else if("remind".equals(type)){
		    		long t = Calendar.getInstance().getTime().getTime();
		    		List<Appointment> appms = appointmentService.getAppointments(new Date(t),new Date(t+24*60*60*1000));
		    		js.put("result",Appointment.capture(appms));
		    	}
		    	test = js.toString();
		    }catch(Exception e){
		    	test = new JSONObject().put("status","123").put("result",new JSONArray().put(new JSONObject())).toString();
		    	e.printStackTrace();
		    }
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }

}
